// pages/outvisit/task/taskDetails.js
var api = require("../../../utils/api.js");
var util = require("../../../utils/util.js");
var common = require("../../../utils/common.js");
const pagetitle = ["", "任务详情", "任务详情", "任务详情", "任务详情"];
const taskDetailsTitles = common.taskDetailsTitles;//详情字段
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: 0,//用户ID
    type: 0,//任务类型
    id: 0,//任务ID
    busname:'',
    completeState:{
      checkinStatus: '',
      uploadImageOutterStatus: '',
      uploadImageInnerStatus: '',
      uploadImageSceneStatus: '',
      uploadVedioStatus: '',
      uploadImageUnclassifiedFilesStatus: '',
      addCaseStatus: '',
      uploadAttachmentStatus: '',
      priceInfoStatus: '',
      landInfoStatus: '',
      decorationInfoStatus: '',
      propertyInfoStatus: '',
      equityInfoStatus: '',
      inputBasicInfoStatus: '',
      locationInfoStatus: '',
      addCase1Status: '',
      addCase2Status: '',
      addCase3Status: ''
    },
    taskDetailsIsHide:true,//任务详情是否隐藏
    taskDetailsList: [],//任务详情列表
    causeState:1,//外访信息状态
    operationIsHide:true,//下方操作按钮是否隐藏
    stopOutvisitFormIsHide:true,//终止任务表单是否隐藏
    suveyColumnList: [],
    detail: {},//查勘详情
    taskAddress:'',
    taskLongitude:'',
    taskLatitude:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userId = wx.getStorageSync("userId");
    var id = options.id;
    var busname = options.busname;

    console.log("options.busname==========>"+busname);
    // 设置标题
    wx.setNavigationBarTitle({
      title: '查勘详情'
    });
    // 设置任务类型和ID
    that.setData({
      userId: userId,
      id: id,
      busname: busname,
    }, () => {
      //console.log("that.data.busname==========>"+that.data.busname);
      // 获取任务详情
      that.getSurveyDetails();
      that.getSurveyColumnComplete();
    });
  },

  // 获取任务详情
  getSurveyDetails:function(){
    var that=this;
    wx.showLoading({
      title: "加载中",
      mask: true
    });//显示加载中
    api.getAppointmentDetails(that.data.id).then(res => {
      if (res.code == 0) {
        wx.hideLoading();

        var data = res.data;
        console.log(data);
        
        // 更新数据
        that.setData({
          detail: data,
          taskAddress: data.address,
        }, () => {
          //console.log("busname==================>"+that.data.busname);
        });
      }
    });
  },

  // 获取任务详情
  getSurveyColumnComplete:function(){
    var that=this;
    wx.showLoading({
      title: "加载中",
      mask: true
    });//显示加载中
    api.getSuveyColumnCompleteStatus(that.data.id).then(res => {
      if (res.code == 0) {
        wx.hideLoading();
        var data = res.data;
        // 更新数据
        that.setData({
          completeState: data,
        }, () => {
            //...
        });
      }
    });
  },

  // 获取年龄
  getAge: function(idCard) {
    var age = "";
    var tmpStr = "";
    if (idCard.length == 15) {
      tmpStr = "19" + idCard.substring(6, 8);
      age = new Date().getFullYear() - parseInt(tmpStr) + "岁";
    }else if(idCard.length == 18){
      tmpStr = idCard.substring(6, 10);
      age = new Date().getFullYear() - parseInt(tmpStr) + "岁";
    }
    return age;
  },

  // 获取性别
  getSex: function(idCard) {
    var sex = "";
    if(idCard && idCard.length >= 18){
      var sexNum = idCard.substring(16, 17);
      if (sexNum != "*") {
        if (sexNum % 2 == 0) {
            sex = "女";
        } else {
            sex = "男";
        }
      }
    }
    return sex;
  },

  //外景照片
  uploadImageOutter: function () {
    var that = this;
    console.log(that.data.id);
    wx.navigateTo({
      url: 'uploadImageOutter?id=' + that.data.id
    });
  },

  //内景照片
  uploadImageInner: function () {
    var that = this;
    wx.navigateTo({
      url: 'uploadImageInner?id=' + that.data.id
    });
  },

  uploadImageUnclassifiedFiles: function () {
    var that = this;
    wx.navigateTo({
      url: 'uploadImageUnclassifiedFiles?id=' + that.data.id
    });
  },

  //现场照片
  uploadImageScene: function () {
    var that = this;
    var url;

    if (that.data.busname == '商业'){
      url = 'uploadImageSceneB?id=' + that.data.id;
    }
    else if (that.data.busname == '办公'){
      url = 'uploadImageSceneW?id=' + that.data.id;
    }

    console.log(url);
    wx.navigateTo({
      url: url
    });
  },

  //附件内容
  uploadAttachment: function () {
    var that = this;
    var url = 'uploadImageAttachmentH?id=' + that.data.id;
    console.log(url);
    wx.navigateTo({
      url: url
    });
  },


  //小视频
  uploadVedio: function () {
    var that = this;
    wx.navigateTo({
      url: 'uploadVedio?id=' + that.data.id
    });
  },


  inputBasicInfo:function(e){
    var that = this;
    var url = '';
    if (that.data.busname == '住宅'){
      url = 'surveyBasicInfoH?id=' + that.data.id;
    }
    else if (that.data.busname == '商业'){
      url = 'surveyBasicInfoB?id=' + that.data.id;
    }
    else if (that.data.busname == '办公'){
      url = 'surveyBasicInfoW?id=' + that.data.id;
    }
    console.log(url);
    wx.navigateTo({
      url: url
    });
  },

  landInfo:function(e){
    var that = this;
    var url = '';
    if (that.data.busname == '住宅'){
      url = 'surveyLandInfoH?id=' + that.data.id;
    }
    else if (that.data.busname == '商业'){
      url = 'surveyLandInfoB?id=' + that.data.id;
    }
    else if (that.data.busname == '办公'){
      url = 'surveyLandInfoW?id=' + that.data.id;
    }
    wx.navigateTo({
      url: url
    });
  },

  propertyInfo:function(e){
    var that = this;
    var url = '';
    if (that.data.busname == '住宅'){
      url = 'surveyPropertyInfoH?id=' + that.data.id;
    }
    else if (that.data.busname == '商业'){
      url = 'surveyPropertyInfoH?id=' + that.data.id;
    }
    else if (that.data.busname == '办公'){
      url = 'surveyPropertyInfoH?id=' + that.data.id;
    }
    wx.navigateTo({
      url: url
    });
  },

  equityInfo:function(e){
    var that = this;
    var url = '';
    if (that.data.busname == '住宅'){
      url = 'surveyEquityInfoH?id=' + that.data.id;
    }
    else if (that.data.busname == '商业'){
      url = 'surveyEquityInfoB?id=' + that.data.id;
    }
    else if (that.data.busname == '办公'){
      url = 'surveyEquityInfoW?id=' + that.data.id;
    }
    wx.navigateTo({
      url: url
    });
  },

  locationInfo:function(e){
    var that = this;
    var url = '';
    console.log(that.data.busname);
    if (that.data.busname == '住宅'){
      url = 'surveyLocationInfoH?id=' + that.data.id;
    }
    else if (that.data.busname == '商业'){
      url = 'surveyLocationInfoB?id=' + that.data.id;
    }
    else if (that.data.busname == '办公'){
      url = 'surveyLocationInfoW?id=' + that.data.id;
    }
    console.log(url);
    wx.navigateTo({
      url: url
    });
  },

  decorationInfo:function(e){
    var that = this;
    var url = '';
    if (that.data.busname == '住宅'){
      url = 'decorationInfoH?id=' + that.data.id;
    }
    else if (that.data.busname == '商业'){
      url = 'decorationInfoB?id=' + that.data.id;
    }
    else if (that.data.busname == '办公'){
      url = 'decorationInfoW?id=' + that.data.id;
    }
    wx.navigateTo({
      url: url
    });
  },

  priceInfo:function(e){
    var that = this;
    var url = '';
    if (that.data.busname == '住宅'){
      url = 'priceInfoH?id=' + that.data.id;
    }
    else if (that.data.busname == '商业'){
      url = 'priceInfoB?id=' + that.data.id;
    }
    else if (that.data.busname == '办公'){
      url = 'priceInfoW?id=' + that.data.id;
    }
    wx.navigateTo({
      url: url
    });
  },

  // 预约信息
  addCase1:function(e){
    var that = this;
    wx.navigateTo({
      url: 'surveyAddCase1?id=' + that.data.id + '&busname=' + that.data.busname
    });
  },

  addCase2:function(e){
    var that = this;
    wx.navigateTo({
      url: 'surveyAddCase2?id=' + that.data.id + '&busname=' + that.data.busname
    });
  },

  addCase3:function(e){
    var that = this;
    wx.navigateTo({
      url: 'surveyAddCase3?id=' + that.data.id + '&busname=' + that.data.busname
    });
  },

  doSubmitData:function (id) {
    var that = this;
    var busname = that.data.busname;
    //选重新获取一次状态表
    that.getSurveyColumnComplete();

    //先作必填项检查
    if(busname == '住宅') {
      if (that.data.completeState.uploadImageUnclassifiedFilesStatus == '未完成' && that.data.completeState.uploadImageOutterStatus == '未完成') {
        wx.showToast({
          title: '[外景照片]未完成，请先完成外景照片！',
          icon: 'none'
        });
        return;
      }
      if (that.data.completeState.uploadImageUnclassifiedFilesStatus == '未完成' && that.data.completeState.uploadImageInnerStatus == '未完成') {
        wx.showToast({
          title: '[内景照片]未完成，请先完成内景照片！',
          icon: 'none'
        });
        return;
      }
    }
    else {
      if (that.data.completeState.uploadImageUnclassifiedFilesStatus == '未完成' && that.data.completeState.uploadImageSceneStatus == '未完成') {
        wx.showToast({
          title: '[现场拍照]未完成，请先完成现场拍照！',
          icon: 'none'
        });
        return;
      }
    }
    if (that.data.completeState.uploadImageUnclassifiedFilesStatus == '未完成' && that.data.completeState.uploadAttachmentStatus == '未完成') {
      wx.showToast({
        title: '[上传附件]未完成，请先完成上传附件！',
        icon: 'none'
      });
      return;
    }
    if (that.data.completeState.priceInfoStatus == '未完成') {
      wx.showToast({
        title: '[价格调查]未完成，请先完成价格调查！',
        icon: 'none'
      });
      return;
    }
    if (that.data.completeState.landInfoStatus == '未完成') {
      wx.showToast({
        title: '[土地实物状况]未完成，请先完成土地实物状况！',
        icon: 'none'
      });
      return;
    }
    if (that.data.completeState.decorationInfoStatus == '未完成') {
      wx.showToast({
        title: '[室内状况]未完成，请先完成室内状况！',
        icon: 'none'
      });
      return;
    }
    if (that.data.completeState.equityInfoStatus == '未完成') {
      wx.showToast({
        title: '[权益状况]未完成，请先完成权益状况！',
        icon: 'none'
      });
      return;
    }
    if (that.data.completeState.inputBasicInfoStatus == '未完成') {
      wx.showToast({
        title: '[建筑物实物状况]未完成，请先完成建筑物实物状况！',
        icon: 'none'
      });
      return;
    }
    if (that.data.completeState.locationInfoStatus == '未完成') {
      wx.showToast({
        title: '[区位状况]未完成，请先完成区位状况！',
        icon: 'none'
      });
      return;
    }

    //执行提交任务
    wx.showLoading({
      title: '提交中'
    });//显示加载中
    api.surveyComplete(that.data.id).then(res => {
      console.log(res);
      wx.hideLoading();

      if (res.code == 0) {
        console.log("查勘信息成功！");

        wx.showToast({
          title: '数据提交成功！',
          icon: 'none'
        });

        wx.navigateTo({
          url: 'taskList?type=2'
        });
      }
    });
  },

  copySuveryHistory: function(id) {
    var that = this;
    wx.navigateTo({
      url: '../../search/searchDetail?type=' + this.data.type +'&id=' + that.data.id
    });
  },

  // 提交表单
  submitSuvery: function (id) {
    var that = this;

    wx.showModal({
      title: "提示",
      content: "确定要完成任务吗？",
      success: function (sm) {
        if (sm.confirm) {
          that.doSubmitData(id);
        }
      }
    })
  },

  // 设置下方操作按钮是否隐藏
  setOperationIsHide: function (type, outvisitTime) {
    var that = this;
    var operationIsHide = true;
    if (type == 1 || type == 2) {
      operationIsHide = false;
    }
    if (type == 3) {
      var myDate = Date.parse(new Date());
      var curTime = util.formatTimeTT(myDate, "Y-M-D");
      if (outvisitTime == curTime){
        operationIsHide = false;
      }
    }
    that.setData({
      operationIsHide: operationIsHide
    });
  },

  // 详情按钮点击事件
  detailsBtnClick: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.id;
    var url="";
    switch(type){
      case '1':
        url = "collectionRecord";
        break;
      case '2':
        url = "totalDebtDetails";
        break;
      default:
        break;
    }
    wx.navigateTo({
      url: url + '?id=' + that.data.collectionCaseId
    });
  },

  // 外访信息切换
  causeTabClick: function (e){
    this.setData({
      causeState: e.currentTarget.dataset.id
    });
  },

  // 任务详情收起
  packUpTask:function(){
    var that=this;
    this.setData({
      taskDetailsIsHide: that.data.taskDetailsIsHide?false:true
    });
  },

  /**
   * 下方操作按钮点击事件
   */
  // 打卡
  checkin: function () {
    var that = this;
    var that = this;
    var busname = that.data.busname;
    wx.navigateTo({
      url: 'signIn?id=' + that.data.id + '&taskLongitude=' + that.data.taskLongitude + '&taskLatitude=' + that.data.taskLatitude + '&taskAddress=' + that.data.taskAddress + '&busname=' + busname
    });
  },

  telephone: function (option) {
    var telnumber = option.currentTarget.dataset.telephone;
    wx.makePhoneCall({
      phoneNumber: telnumber
    })
  },

  // 录入信息
  inputInfo:function(){
    var that = this;
    wx.navigateTo({
      url: 'inputInfo?id=' + that.data.id + '&collectionCaseId=' + that.data.collectionCaseId + '&trustor=' + that.data.trustor
    });
  },

  // 提交表单
  formSubmit: function (e) {
    var that = this;
    if (that.data.isNoUpdate){
      wx.showModal({
        title: "提示",
        content: "确定要修改备注吗？",
        success: function (sm) {
          if (sm.confirm) {
            // 提交表单数据
            that.formSubmitData(e);
          }
        }
      })
    } else {
      // 提交表单数据
      that.formSubmitData(e);
    }
  },

  // 提交表单数据
  formSubmitData: function (e) {
    var that = this;
    var data = e.detail.value;
    var submitData = {};
    for (var d in data) {
      submitData[d] = data[d];
    }
    console.log(submitData);

    if (submitData.remark == '') {
      wx.showToast({
        title: "请填写备注内容",
        icon: "none"
      });
      return;
    }

    wx.showLoading({
      title: '提交中'
    });//显示加载中
    api.saveSurveyRemarkApp(submitData).then(res => {
      if (res.code == 0) {
        wx.hideLoading();
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
